import { ResponseData, SUCCESS_CODE } from '@/utils/request';
import type { RequestData } from '@ant-design/pro-table';
import type { AddDeviceItem, DeviceItem, UpdateDeviceItem } from './types';
import { message } from 'antd';
import { request } from 'umi';

//分页查询设备列表
export async function pageQueryDevice(params: {
  tenant_id?: string,
  server_status?: number,
  road_name?: string,
  brand?: string,
  device_no?: string,
  page?: number,
  pageSize?: number,
}): Promise<RequestData<DeviceItem>> {
  const res = await request('/device/view', {
    params: {
      page: 1,
      pageSize: 10,
      ...params,
    },
  });
  if (!res || res.code !== SUCCESS_CODE) {
    message.error('获取失败');
    return { success: false, total: 0, data: [] };
  }
  return {
    success: true,
    total: res.data.total,
    data: res.data.list,
  };
}

//注销设备
export async function deleteDevice(data: {
  device_no: string
}): Promise<ResponseData<any>> {
  const res = await request('/device/unregister', {
    method: 'POST',
    data: {
      device_no: data.device_no
    }
  });
  if (res) return { code: res.code, data: res.data, success: true, message: res.message };
  return res;
}

//注册设备
export async function addDevice(data: AddDeviceItem): Promise<RequestData<any>> {
  const res = await request('/device/register', {
    method: 'POST',
    data: data
  });

  return { code: res.code, data: res.data, success: true, message: res.message };

}

//更新设备
export async function updateDevice(
  data: UpdateDeviceItem,
): Promise<RequestData<any>> {
  const res = await request('/device/update', {
    method: 'POST',
    data: data
  });

  return { code: res.code, data: res.data, success: true, message: res.message };

}
